package com.wenqiner.requesttool.util;

import cn.hutool.core.text.CharSequenceUtil;
import com.wenqiner.requesttool.core.constants.FileConstant;
import com.wenqiner.requesttool.domain.Table;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author rhf
 * @date 2023-02-28
 **/
public class TableUtil {

    /**
     * 解析表格
     *
     * @param gridPane gridPane
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author rhf
     */
    public static Map<String, String> parseHeader(GridPane gridPane) {
        ObservableList<Node> children = gridPane.getChildren();
        int maxRows = 0;
        for (Node node : children) {
            Integer rowIndex = GridPane.getRowIndex(node);
            if (null == rowIndex) {
                rowIndex = 0;
            }
            if (maxRows < rowIndex) {
                maxRows = rowIndex;
            }
        }
        List<Table> tables = new ArrayList<>();
        for (int i = 0; i <= maxRows; i++) {
            Table table = new Table();
            tables.add(table);
            for (Node node : children) {
                if (!(node instanceof TextField)) {
                    continue;
                }

                Integer rowIndex = GridPane.getRowIndex(node);
                Integer columnIndex = GridPane.getColumnIndex(node);
                if (null == rowIndex) {
                    rowIndex = 0;
                }
                if (i == rowIndex) {
                    TextField field = ((TextField) node);
                    if (null == columnIndex || 0 == columnIndex) {
                        table.setKey(field.getText());
                    }
                    if (null != columnIndex && 1 == columnIndex) {
                        table.setValue(field.getText());
                    }
                }
            }
        }
        Collections.reverse(tables);
        return tables.stream()
                .filter(t -> CharSequenceUtil.isNotBlank(t.getKey()))
                .filter(distinctByKey(Table::getKey))
                .collect(Collectors.toMap(Table::getKey, Table::getValue));
    }


    /**
     * 转换系统参数
     *
     * @param params k,v
     * @return java.util.Map<java.lang.String, java.lang.String>
     * @author rhf
     */
    public static Map<String, String> convertSystemParam(Map<String, String> params) {
        if (null == params) {
            return null;
        }
        Map<String, String> convertMap = new ConcurrentHashMap<>();
        params.keySet()
                .forEach(k -> {
                    convertMap.put(FileConstant.PARAM_PREFIX.concat(k).concat(FileConstant.PARAM_SUFFIX), params.get(k));
                });
        return convertMap;
    }

    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
